package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.bean.DataGridView;
import com.woniuxy.bean.UserVo;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String telephone, String password) {
        User result=this.userMapper.selectUserByTelephone(telephone);
        if(result!=null){
            if(result.getPassword()!=null&&result.getPassword().equals(password)){
                return result;
            }
        }
        return null;
    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public DataGridView<User> findUsersByPage(UserVo userVo) {
        DataGridView<User> dataGridView=new DataGridView<>();
        //必须要使用Page对象进行接收
        Page page=PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<User> userList=this.userMapper.selectUsersByUser(userVo);
        //然后在将page对象中getTotal()获取的值再复制为DataGridView中count
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(userList);
        return dataGridView;
    }
    @Transactional
    @Override
    public void addUser(User user) throws RuntimeException{
        User user1=this.userMapper.selectUserByTelephone(user.getTelephone());
        if(user1!=null){
            throw new RuntimeException("用户已存在");
        }
        this.userMapper.insertUser(user);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserById(int id) {
        //在删除用户的时候，由于用户表设置了外键约束，所以先删除引用表的信息，方能删除用户信息
        this.userMapper.deleteUserRoleByUid(id);
        this.userMapper.deleteUserById(id);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteBatch(String ids) {
        if(ids!=null){
            String[] idArray=ids.split(",");
            if(idArray!=null&&idArray.length>0){
                for(String idStr:idArray){
                    int id=Integer.parseInt(idStr);
                    this.deleteUserById(id);
                }
            }
        }

    }

    @Override
    public User findUserById(int id) {
        return this.userMapper.selectUserById(id);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void assignRole(Integer uid, Integer[] roles) {
        this.userMapper.deleteUserRoleByUid(uid);
        if(roles!=null&&roles.length>0){
            for(Integer rid:roles){
                this.userMapper.insertUserRole(uid,rid);
            }
        }
    }
    @Override
    public User selectUserByTelephone(String telephone) {
        return this.userMapper.selectUserByTelephone(telephone);
    }

    @Override
    public Set<String> selectRolesNamesByTelephone(String telephone) {
        return this.userMapper.selectRolesNamesByTelephone(telephone);
    }

    @Override
    public Set<String> selectPerCodesByTelephone(String telephone) {
        return this.userMapper.selectPerCodesByTelephone(telephone);
    }
}




















